//
//  SGTextField.m
//  SGOCFrame
//
//  Created by lyq on 2019/3/25.
//  Copyright © 2019 蜀归科技. All rights reserved.
//

#import "ZMTextField.h"

@interface ZMTextField ()<UITextFieldDelegate>

/** <#name#>*/
@property (strong, nonatomic) UIView *txtLeftView;
@property (strong, nonatomic) UIView *txtRightView;

@end

@implementation ZMTextField

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.delegate = self;
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.delegate = self;
}

- (void)setLeftViewWidth:(CGFloat)leftViewWidth{
    self.txtLeftView    = [[UIView alloc] initWithFrame:CGRectMake(0, 0, leftViewWidth, self.frame.size.height)];
    self.leftView       = self.txtLeftView;
    self.leftViewMode   = UITextFieldViewModeAlways;
}

- (void)setLeftImage:(UIImage *)leftImage{
    UIImageView *img= [[UIImageView alloc] initWithFrame:self.leftView.bounds];
    img.image       = leftImage;
    img.contentMode = UIViewContentModeCenter;
    [self.txtLeftView addSubview:img];
}

- (void)setRightViewWidth:(CGFloat)rightViewWidth{
    self.txtRightView   = [[UIView alloc] initWithFrame:CGRectMake(0, 0, rightViewWidth, self.frame.size.height)];
    self.rightView      = self.txtRightView;
    self.rightViewMode  = UITextFieldViewModeAlways;
}

- (void)setRightImage:(UIImage *)rightImage{
    UIImageView *img= [[UIImageView alloc] initWithFrame:self.rightView.bounds];
    img.image       = rightImage;
    img.contentMode = UIViewContentModeCenter;
    img.userInteractionEnabled = YES;
    UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
    [singleTapGestureRecognizer setNumberOfTapsRequired:1];
    [img addGestureRecognizer:singleTapGestureRecognizer];
    [self.txtRightView addSubview:img];
}

- (void)singleTap:(UIGestureRecognizer*)gestureRecognizer{
    [self becomeFirstResponder];
}

- (void)setMaxLength:(NSUInteger)maxLength{
    _maxLength = maxLength;
    if (maxLength != 0) {
        [self addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    }
}

- (void)setPlaceHolderColor:(UIColor *)PlaceHolderColor{
    ((UILabel *)[self valueForKey:@"placeholderLabel"]).textColor = PlaceHolderColor;
}

- (UIColor *)placeHolderColor{
    return ((UILabel *)[self valueForKey:@"placeholderLabel"]).textColor;
}
//
//- (void)setIsPrice:(BOOL)isPrice{
//    self.delegate = self;
//    _isPrice = isPrice;
//}
//
//- (void)setIsNumber:(BOOL)isNumber{
//    self.delegate = self;
//    _isNumber = isNumber;
//}
//
//- (void)setIsNumberAndLetter:(BOOL)isNumberAndLetter{
//    self.delegate = self;
//    _isNumberAndLetter = isNumberAndLetter;
//}
//
//- (void)setIsIdCard:(BOOL)isIdCard{
//    self.delegate = self;
//    _isIdCard = isIdCard;
//}
//
//- (void)setIsPassword:(BOOL)isPassword{
//    self.delegate = self;
//    _isPassword = isPassword;
//}

- (void)textChange:(UITextField *)textField{
    int bytes = [self stringConvertToInt:textField.text];
    if (bytes > _maxLength){
        textField.text = self.lastTextContent;
    }else{
        self.lastTextContent = textField.text;
    }
}
//得到字节数函数
- (int)stringConvertToInt:(NSString*)strtemp{
    int strlength = 0;
    char *p = (char*)[strtemp cStringUsingEncoding:NSUnicodeStringEncoding];
    for (int i=0 ; i<[strtemp lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++){
        if (*p) {
            p++;
            strlength++;
        }else {
            p++;
        }
    }
    return strlength;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (string.length == 0) {
        //删除
        return YES;
    }
    if (_isPrice) {
        NSString *regex = @"^[0-9.]*$";
        NSPredicate *priceTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        if ([priceTest evaluateWithObject:string]) {
            if ([textField.text containsString:@"."]) {
                //确保只输入一个小数点
                if ([string isEqualToString:@"."]) {
                    return NO;
                }
                if (textField.text.length - [textField.text rangeOfString:@"."].location > 2) {
                    //确保只输入2位小数
                    return NO;
                }
            }
            if (([textField.text isEqualToString:@"0"] && ![string isEqualToString:@"."]) || [textField.text isEqualToString:@"."]) {
                //清除第一位是0，或者小数点
                textField.text = @"";
            }
        } else {
            return NO;
        }
    }else if (_isNumber){
        NSString *regex = @"^[0-9]+$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        return [pred evaluateWithObject:string];
    }else if (_isNumberAndLetter){
        NSString *regex = @"^[A-Za-z0-9]+$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        return [pred evaluateWithObject:string];
    }else if (_isIdCard){
        if (range.location < 17) {
            NSString *regex = @"^[0-9]+$";
            NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
            return [pred evaluateWithObject:string];
        }
        NSString *regex = @"^[0-9Xx]+$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        return [pred evaluateWithObject:string];
    }else if (_isPassword){
        NSString *regex = @"^[^\\u4e00-\\u9fa5]*$";
        NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        return ([pre evaluateWithObject:string] && ![string isEqualToString:@" "]);
    }
    return YES;
}

- (void)setFirstResponder:(BOOL)firstResponder{
    if (firstResponder) {
        [self becomeFirstResponder];
    }
}



@end
